<template>
  <view class="u-p-t-28 main-pd">
    <view class="u-flex u-flex-end">
      <view class="color-f34b47 font-28 u-m-b-20" @click="$u.route('/pagesB/yzzz/applyList')">申请记录</view>
    </view>
    <view class="u-card u-m-t-20">
      <view class="font-28 color-595959 u-p-b-20">小区名称</view>
       <u-input
      placeholder="请输入内容"
      v-model="apply.xqName"
       :maxlength="20"
      border="none"
    ></u-input>
    </view>
    <view class="u-card u-m-t-20">
      <view class="font-28 color-595959 u-p-b-20"><text class="color-f34b47">*</text> 请填写申请书内容</view>
      <u-textarea
        border="none"
        v-model="apply.content"
        :maxlength="100"
        height="200rpx"
        placeholder="请输入..."></u-textarea>
    </view>
    <view class="u-card u-m-t-20">
      <view class="font-28 color-595959">业主签字文件</view>
      <view class="u-p-t-12">
        <cl-upload
          v-model="signList"
          :max="1"
          useBeforeDelete
           cloudType="other"
          @beforeDelete="delItem"
          :action="config.baseUrl + '/publicapi/upload'"
          @onSuccess="onSuccess($event,0)"></cl-upload>
      </view>
    </view>
    <view class="u-card u-m-t-20">
      <view class="font-28 color-595959">其他相关文件</view>
      <view class="u-p-t-12">
        <cl-upload
          v-model="otherList"
          :max="1"
          useBeforeDelete
           cloudType="other"
          @beforeDelete="delOther"
          :action="config.baseUrl + '/publicapi/upload'"
          @onSuccess="onSuccess($event,1)"></cl-upload>
      </view>
    </view>
    <view class="u-m-t-20 u-card normal-cell">
      <u-cell-group :border="false">
        <u-cell
          title="申请时间"
          :value="apply.applyTime"></u-cell>
        <u-cell title="申请人">
          <template #value>
            <u-input
              placeholder="请输入"
              inputAlign="right"
              border="none"
              v-model="apply.applyUser"></u-input>
          </template>
        </u-cell>
        <u-cell title="联系方式" :value="apply.phone" :border="false"></u-cell>
      </u-cell-group>
    </view>
    <view class="u-m-t-50 u-p-b-50">
        <u-button text="提交" color="linear-gradient(90deg, #FA827D 0%, #F34B47 100%)" :customStyle="{
            fontSize: '36rpx',
            height: '96rpx',
            }" @click="applyHandle"></u-button>
    </view>
  </view>
</template>

<script setup>
import { ref, reactive } from "vue";
import useInfoStore from "@/store/user";
const userStore = useInfoStore();
import config from "@/config";
import {applyywhApi} from "@/api/index"
let signList = ref([]);
let otherList = ref([]);
// 上传处理
const onSuccess = (res,type) => {
    // 0 业主签字 1 其他
  let data = res.data;
  let result = data.map((item) => {
    return {
      path: config.baseUrl + item.fileurl,
      filename: item.filename,
    };
  });
  if(type==0){
    signList.value.push(...result);
}else{
      otherList.value.push(...result);
  }
};
// 删除签名文件
const delItem = (item, index, next) => {
    signList.value.splice(index, 1);
  next();
};
// 删除其他文件
const delOther = (item, index, next) => {
    otherList.value.splice(index, 1);
  next();
};
let apply = reactive({
  applyUser: userStore.dzkName,
  xqName: userStore.deptName,
  content: "",
  deptId: userStore.deptId,
  dzyzkId: userStore.dzyzkId,
  openId: userStore.openId,
  phone: userStore.dzkTel,
  otherfile:'',
  qzfile:'',
  applyTime:uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM:ss')
});
const applyHandle = ()=>{
  if(signList.value.length==0){
    uni.$u.toast('请上传业主签字文件');
    return false;
  }
  if (apply.content ==""){
    uni.$u.toast('请填写申请书内容');
    return false;
  }
    apply.qzfile = signList.value.length?signList.value[0].path:'';
    apply.otherfile = otherList.value.length?otherList.value[0].path:'';
    applyywhApi(apply).then(res=>{
        if(res.success){
            uni.$u.toast("提交成功");
            signList.value = [];
            otherList.value = [];
            apply.qzfile = "",
            apply.otherfile = ""
            apply.content = ""
            apply.applyUser = userStore.dzkName
            apply.phone = userStore.dzkTel
            
        }else{
            uni.$u.toast(res.msg);
        }
    })
}
</script>

<style lang="scss" scoped></style>
